﻿/*************************************************
该文件用于控制所有谷歌地球的对象以及方法
创建日期 : 2010-10-12
创建者 : BILL
**************************************************/


/*****************初始化谷歌地球************************/
var MeiEarth = function(mapContainClass) {
    this.ge = null; //谷歌地球实例
    this.interval = null;
    this.isStartDrawOnEarth = false; //判断是否开始绘画电子围栏
    this.earthGEOCircleCenterLatitude = 0;
    this.earthGEOCircleCenterLongitude = 0;
    this.historypt = null;
    this.geoEarth3D = null;
    this.poiArray = [];
    this.earthLine = null;

    this.mapcontainclass = mapContainClass;

    this.flagmarker = null;
    this.addoreditpoi = null;
    this.geofencecolor = "00ffff";
    meiearthinstance = this;

    this.DS_simulator = null;
    this.DS_points = [];
    this.DS_geHelpers;
    this.DS_placemarks = [];
    this.DS_routes = [];
    this.DS_step = 0;
    this.DS_speed = 2.5;
    this.runcarinterval = null;

    this.realtimeTrackingLine = null;
    this.realtimeoverlayarray = [];
    this.realtimepoints = [];
    
    this.EarthLoad();

}
MeiEarth.prototype = {
    EarthLoad: function () {

        var containclass = this.mapcontainclass;
        var itself = this;
        this.mapcontainclass.map.getEarthInstance(function (instance) {
            if (!instance) {
                containclass.earthState = EnumEarthState.Unknown;
                return;
            }

            itself.ge = instance;
            itself.DS_geHelpers = new GEHelpers(itself.ge);
            google.earth.addEventListener(itself.ge.getGlobe(), 'click', function (event) {
                itself.EarthOnClick(event, containclass);
            });
            google.earth.addEventListener(itself.ge.getGlobe(), 'mousemove', function (event) {
                itself.EarthMouseMove(event, containclass);
            });
            containclass.earthState = EnumEarthState.Done;
            containclass.toInitAnimateForChangeMapType();
            containclass.resetStartBtn(containclass.startbtn, containclass.stop, containclass.contin);
            itself.ShowAllMarkerOnEarth(containclass.mapLatLngToRealLatLng(containclass.map.getCenter()));
        });
    },
    EarthOnClick: function (event, containClass) {
        if (this.isStartDrawOnEarth) {
            this.isStartDrawOnEarth = false;
            var p1 = new GLatLng(this.earthGEOCircleCenterLatitude, this.earthGEOCircleCenterLongitude);
            var p2 = new GLatLng(event.getLatitude(), event.getLongitude());
            var distance = p1.distanceFrom(p2);
            switch (containClass._maptooltype) {
                case EnumMapToolType.GeoFenceInAlarm:
                case EnumMapToolType.GeoFenceOutAlarm:
                    containClass.FnGeoFenceCallBack(this.earthGEOCircleCenterLatitude, this.earthGEOCircleCenterLongitude, event.getLatitude(), event.getLongitude());
                    break;
                case EnumMapToolType.CircleGeoFenceInAlarm:
                case EnumMapToolType.CircleGeoFenceInOutAlarm:
                case EnumMapToolType.CircleGeoFenceOutAlarm:
                    containClass.FnCircleGeoFenceCallBack(this.earthGEOCircleCenterLatitude, this.earthGEOCircleCenterLongitude, distance);
                    break;
            }
            containClass._maptooltype = EnumMapToolType.Pan;
            this.ge.getFeatures().removeChild(this.geoEarth3D);
            this.geoEarth3D = null;
        }
        else {
            if (event) {
                this.earthGEOCircleCenterLatitude = event.getLatitude();
                this.earthGEOCircleCenterLongitude = event.getLongitude();
                switch (containClass._maptooltype) {
                    case EnumMapToolType.GeoFenceInAlarm:
                        this.geofencecolor = "0000ff";
                        this.isStartDrawOnEarth = true;
                        break;
                    case EnumMapToolType.GeoFenceOutAlarm:
                        this.geofencecolor = "00ff00";
                        this.isStartDrawOnEarth = true;
                        break;
                    case EnumMapToolType.CircleGeoFenceInAlarm:
                        this.geofencecolor = "0000ff";
                        this.isStartDrawOnEarth = true;
                        break;
                    case EnumMapToolType.CircleGeoFenceInOutAlarm:
                        this.geofencecolor = "00ffff";
                        this.isStartDrawOnEarth = true;
                        break;
                    case EnumMapToolType.CircleGeoFenceOutAlarm:
                        this.geofencecolor = "00ff00";
                        this.isStartDrawOnEarth = true;
                        break;
                    case EnumMapToolType.AddPOI:
                        this.DrawAddOrEditPoi(event.getLatitude(), event.getLongitude(), containClass._submitcap, containClass._cancelcap);
                        containClass._maptooltype = EnumMapToolType.pan;
                        this.addoreditpoi = null;
                        break;
                    case EnumMapToolType.BeginEditMovePOI:
                        containClass._maptooltype = EnumMapToolType.pan;
                        this.addoreditpoi = null;
                        break;
                }
            }
        }
    },
    EarthMouseMove: function (event, containClass) {
        if (this.isStartDrawOnEarth) {
            switch (containClass._maptooltype) {
                case EnumMapToolType.GeoFenceInAlarm:
                case EnumMapToolType.GeoFenceOutAlarm:
                    if (this.geoEarth3D)
                        this.ge.getFeatures().removeChild(this.geoEarth3D);
                    this.geoEarth3D = this.CreateRectanglePolygon(this.earthGEOCircleCenterLatitude, this.earthGEOCircleCenterLongitude, event.getLatitude(), event.getLongitude(), this.geofencecolor);
                    this.ge.getFeatures().appendChild(this.geoEarth3D);
                    break;
                case EnumMapToolType.CircleGeoFenceInAlarm:
                case EnumMapToolType.CircleGeoFenceInOutAlarm:
                case EnumMapToolType.CircleGeoFenceOutAlarm:
                    var p1 = new GLatLng(this.earthGEOCircleCenterLatitude, this.earthGEOCircleCenterLongitude);
                    var p2 = new GLatLng(event.getLatitude(), event.getLongitude());
                    var distance = p1.distanceFrom(p2);

                    var ss = Math.sqrt(Math.pow(event.getLatitude() - this.earthGEOCircleCenterLatitude, 2) + Math.pow(event.getLongitude() - this.earthGEOCircleCenterLongitude, 2)) * 1000;
                    if (this.geoEarth3D)
                        this.ge.getFeatures().removeChild(this.geoEarth3D);
                    this.geoEarth3D = this.CreateCirclePolygon({ latitude: this.earthGEOCircleCenterLatitude,
                        longitude: this.earthGEOCircleCenterLongitude,
                        accuracy: distance,
                        color: this.geofencecolor
                    });
                    this.ge.getFeatures().appendChild(this.geoEarth3D);
                    break;
            }
        }
        else {
            switch (containClass._maptooltype) {
                case EnumMapToolType.BeginEditMovePOI:
                    var poiid = containClass._movePOIInfo.poiid;
                    var name = containClass._movePOIInfo.name;
                    this.DrawAddOrEditPoi(event.getLatitude(), event.getLongitude(), containClass._submitcap, containClass._cancelcap, poiid, name);
                    break;
            }
        }
    },

    ShowAllMarkerOnEarth: function (latlng) {
        this.ClearAllOfEarthMarker();
        this.SolveExcursionFromMap(latlng);
    },



    CreateEarthMarker: function (point, imgsrc, name, WIN_HTML) {
        var itselfge = this.ge;
        var gePoint = this.ge.createPoint('');
        gePoint.setLatitude(point.lat());
        gePoint.setLongitude(point.lng());
        var marker = this.ge.createPlacemark('');
        marker.setName(name);
        var icon = this.ge.createIcon('');
        var style = this.ge.createStyle('');
        var url = window.location.href;
        var lastindex = url.lastIndexOf('/');
        var path = url.substring(0, lastindex);
        if (path.substr(path.length - 1, 1) != '/')
            path = path + '/';
        icon.setHref(path + imgsrc);
        style.getIconStyle().setIcon(icon);
        marker.setStyleSelector(style);
        marker.setGeometry(gePoint);
        if (WIN_HTML) {
            google.earth.addEventListener(marker, 'click', function (event) {
                // Prevent the default balloon from popping up.
                event.preventDefault();
                var balloon = itselfge.createHtmlStringBalloon('');
                balloon.setFeature(marker);
                balloon.setMaxWidth(400);
                balloon.setContentString(WIN_HTML);
                itselfge.setBalloon(balloon);
            });
        }
        else {
            google.earth.addEventListener(marker, 'click', function (event) {
                event.preventDefault();
            });
        }
        return marker;
    },

    CreateEarthLine: function (pointarray) {
        var lineStringPlacemark = this.ge.createPlacemark('');
        lineStringPlacemark.setName('EarthDirection');
        var lineString = this.ge.createLineString('');
        lineStringPlacemark.setGeometry(lineString);
        lineString.setExtrude(true);
        lineString.setAltitudeMode(this.ge.ALTITUDE_RELATIVE_TO_GROUND);

        for (var i = 0; i < pointarray.length; i++) {
            lineString.getCoordinates().pushLatLngAlt(pointarray[i].lat(), pointarray[i].lng(), 0);
        }
        lineStringPlacemark.setStyleSelector(this.ge.createStyle(''));
        var lineStyle = lineStringPlacemark.getStyleSelector().getLineStyle();
        lineStyle.setWidth(5);
        lineStyle.getColor().set('9900ffff');
        google.earth.addEventListener(lineStringPlacemark, 'click', function (event) {
            event.preventDefault();
        });
        return lineStringPlacemark;
    },

    CreateHistoryLine: function (pointarray, meiearthinstance) {
        if (meiearthinstance.mapcontainclass.earthState == EnumEarthState.Unknown) return;
        var lineStringKml = '<LineString><coordinates>\n';
        lineStringKml += pointarray[0].lng().toString() + ',' + pointarray[0].lat().toString() + ',10\n'
                 + pointarray[1].lng().toString() + ',' + pointarray[1].lat().toString() + ',10\n';
        lineStringKml += '</coordinates></LineString>';
        var routeLineString = meiearthinstance.ge.parseKml(lineStringKml);
        routeLineString.setTessellate(true);
        var routePlacemark = meiearthinstance.ge.createPlacemark('');
        routePlacemark.setGeometry(routeLineString);
        routePlacemark.setStyleSelector(meiearthinstance.DS_geHelpers.createLineStyle({ width: 10, color: '88ff0000' }));
        return routePlacemark;
    },

    ClearAllOfEarthMarker: function () {
        if (this.ge) {
            var features = this.ge.getFeatures();
            for (var i = features.getChildNodes().getLength() - 1; i >= 0; i--) {
                var node = features.getChildNodes().item(i);
                if (node.getName() == '' && node != this.addoreditpoi)
                    features.removeChild(node);
            }
        }
    },

    getDefaultImageUrl: function (trackerinfo) {
        var s = trackerinfo.devicetype;
        var direction = trackerinfo.direction;
        var url = "blue.gif";

        if (s == 1 || s == 2 || s == 3 || s == 4 || s == 5) {
            url = "man.gif";
        }
        else {
            var baseImagePath;
            if (trackerinfo.gsminfo != null) {
                url = "car.gif";
            }
            else {
                baseImagePath = "car_";

                if (direction == "East") {
                    baseImagePath += "n.png";
                }
                if (direction == "North") {
                    baseImagePath += "n.png";
                }
                if (direction == "South") {
                    baseImagePath += "n.png";
                }
                if (direction == "West") {
                    baseImagePath += "n.png";
                }
                url = direction;
            }
        }
        return url;
    },

    CreatePlaceMarker: function (point, trackerinfo, labelString) {
        if (this.ge) {
            var direction = trackerinfo.direction;
            var speedkmh = parseInt(trackerinfo.speed);
            var s = trackerinfo.devicetype;
            var imgsrc = trackerinfo.iconname != "" ? "images/trackericon/" + trackerinfo.iconname + "_" : "images/trackericon/" + this.getDefaultImageUrl(trackerinfo);

            if (trackerinfo.iconname != "") {

                if (direction == "East") {
                    imgsrc += "n.png";
                }
                if (direction == "North") {
                    imgsrc += "n.png";
                }
                if (direction == "South") {
                    imgsrc += "n.png";
                }
                if (direction == "West") {
                    imgsrc += "n.png";
                }
                else {
                    imgsrc = "images/trackericon/" + trackerinfo.iconname;
                    imgsrc += ".png";
                }


                //                if (direction >= 337.5 || direction < 22.5)
                //                    imgsrc += "n.png";
                //                else if (direction >= 22.5 && direction < 67.5)
                //                    imgsrc += "ne.png";
                //                else if (direction >= 67.5 && direction < 112.5)
                //                    imgsrc += "e.png";
                //                else if (direction >= 112.5 && direction < 157.5)
                //                    imgsrc += "se.png";
                //                else if (direction >= 157.5 && direction < 202.5)
                //                    imgsrc += "s.png";
                //                else if (direction >= 202.5 && direction < 247.5)
                //                    imgsrc += "sw.png";
                //                else if (direction >= 247.5 && direction < 292.5)
                //                    imgsrc += "w.png";
                //                else if (direction >= 292.5 && direction < 337.5)
                //                    imgsrc += "nw.png";
            }
            if (trackerinfo.gsminfo != null) {
                this.ge.getFeatures().appendChild(this.CreateCirclePolygon({
                    latitude: point.lat(),
                    longitude: point.lng(),
                    accuracy: trackerinfo.gsminfo.accuracy / 2,
                    color: 'd0e040'
                }));
            }

            return this.CreateEarthMarker(point, imgsrc, labelString);
        }
        else {
            return null;
        }
    },
    CreateEarthTrackerMarker: function (point, directionString, name, html) {
        var icon = "blue_";
        var direction = directionString;

        if (direction == "East") {
            icon+="e.png";
        }
        if (direction == "North") {
            icon += "n.png";
        }
        if (direction == "South") {
            icon += "s.png";
        }
        if (direction == "West") {
            icon += "w.png";
        }

        var marker = this.CreateEarthMarker(point, icon, name, html);
        return marker;

    },
    RemoveMarker: function (marker) {
        if (this.ge && marker != null) {
            this.ge.getFeatures().removeChild(marker);
        }
    },

    AddMarker: function (marker) {
        if (this.ge) {
            this.ge.getFeatures().appendChild(marker);
        }
    },

    SolveExcursionFromMap: function (latlng) {
        this.mapcontainclass.map.setCenter(latlng);
    },

    DrawPoi: function (point, name) {
        if (this.ge) {
            this.ClearAllOfEarthMarker();
            if (this.IsPoiArrayHasPoi(this.poiArray, point, name))
                return;
            var marker = this.CreateEarthMarker(point, "images/poi32.gif", name);
            this.poiArray.push(marker);
            this.ge.getFeatures().appendChild(marker);
        }
    },

    HideAllPoi: function () {
        if (this.ge) {
            for (var i = this.poiArray.length - 1; i >= 0; i--) {
                this.ge.getFeatures().removeChild(this.poiArray[i]);
            }
            this.poiArray.clear();
        }
    },
    HideBalloon: function () {
        if (this.ge)
            this.ge.setBalloon(null);
    },

    DrawAddOrEditPoi: function (lat, lng, submit, cancel, poiid, name) {
        //        this.ClearAllOfEarthMarker();
        var pt = new GLatLng(lat, lng);
        var fn = !poiid ? 'submitAddPOI(\'\',' + pt.lat().toString() + ',' + pt.lng().toString() + ',' + 0 + ')' : 'submitEditPOI(' + poiid + ',\'\',' + pt.lat().toString() + ',' + pt.lng().toString() + ',' + 0 + ')';
        var WIN_HTML = '<div style="width: 12em; style: font-size: small"><INPUT id="textpoi" name="textpoi" value="' + (name ? name : "") + '"><INPUT id=butAddPOI type=button value=';
        WIN_HTML += submit + ' name=submit onclick="return ' + fn + '">&nbsp;<INPUT id=butCancel type=button value=';
        WIN_HTML += cancel + ' name=cancel onclick="cancelAddEditPOI()"></div>';
        if (this.addoreditpoi)
            this.ge.getFeatures().removeChild(this.addoreditpoi);
        this.addoreditpoi = this.CreateEarthMarker(pt, "images/poi32b.gif", "", WIN_HTML);
        this.ge.getFeatures().appendChild(this.addoreditpoi);
    },

    IsPoiArrayHasPoi: function (poiarray, point, name) {
        for (var i = 0; i < poiarray.length; i++) {
            var geolat = poiarray[i].getGeometry().getLatitude();
            var geolng = poiarray[i].getGeometry().getLongitude();
            var geoname = poiarray[i].getName();
            if (geolat == point.lat() && geolng == point.lng() && geoname == name)
                return true;
        }
        return false;
    },

    CreateCirclePolygon: function (coords) {
        if (this.ge) {
            var center = new geo.Point(coords.latitude, coords.longitude);
            var ring = this.ge.createLinearRing('');
            for (var heading = 0; heading <= 360; heading += 10) {
                var circlePt = center.destination({ heading: heading,
                    distance: coords.accuracy
                });
                ring.getCoordinates().pushLatLngAlt(circlePt.lat(), circlePt.lng(), 0);
            }
            var polygonPlacemark = this.ge.createPlacemark('');
            polygonPlacemark.setName('i am here');
            polygonPlacemark.setGeometry(this.ge.createPolygon(''));
            var outer = this.ge.createLinearRing('');

            polygonPlacemark.getGeometry().setOuterBoundary(ring);
            polygonPlacemark.setStyleSelector(this.ge.createStyle(''));
            var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
            lineStyle.setWidth(5);
            lineStyle.getColor().set('99' + coords.color);


            var polyStyle = polygonPlacemark.getStyleSelector().getPolyStyle();
            polyStyle.getColor().set('50' + coords.color);
            google.earth.addEventListener(polygonPlacemark, 'click', function (event) {
                event.preventDefault();
            });
            return polygonPlacemark;

        }

    },

    CreateRectanglePolygon: function (lat1, lng1, lat2, lng2, color) {
        var ring = this.ge.createLinearRing('');
        ring.getCoordinates().pushLatLngAlt(lat1, lng1, 0);
        ring.getCoordinates().pushLatLngAlt(lat2, lng1, 0);
        ring.getCoordinates().pushLatLngAlt(lat2, lng2, 0);
        ring.getCoordinates().pushLatLngAlt(lat1, lng2, 0);
        var polygonPlacemark = this.ge.createPlacemark('');
        polygonPlacemark.setGeometry(this.ge.createPolygon(''));
        var outer = this.ge.createLinearRing('');
        polygonPlacemark.getGeometry().setOuterBoundary(ring);
        polygonPlacemark.setStyleSelector(this.ge.createStyle(''));
        var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
        lineStyle.setWidth(5);
        lineStyle.getColor().set('99' + color);
        var polyStyle = polygonPlacemark.getStyleSelector().getPolyStyle();
        polyStyle.getColor().set('50' + color);
        google.earth.addEventListener(polygonPlacemark, 'click', function (event) {
            event.preventDefault();
        });
        return polygonPlacemark;

    },

    ShowDirectionInEarth: function (linepointarray, earth) {

        if (this.ge) {
            if (this.earthLine)
                this.ge.getFeatures().removeChild(this.earthLine);
            this.earthLine = this.CreateEarthLine(linepointarray);
            this.ge.getFeatures().appendChild(this.earthLine);
            setTimeout(function () { earth.ClearAllOfEarthMarker(); }, 50);

        }

    },

    DrawFlagMarker: function (pt, html) {
        if (this.ge) {
            if (this.flagmarker)
                this.ge.getFeatures().removeChild(this.flagmarker);
            this.flagmarker = this.CreateEarthMarker(pt, "images/red.png", html, html);
            this.ge.getFeatures().appendChild(this.flagmarker);
            this.ClearAllOfEarthMarker();
        }
    },
    RunInAccordanceWithTrackHistory: function () {

    },

    BeginRunCar: function () {
        this.DS_points.clear();
        for (var i = 0; i < this.mapcontainclass.History_points.length; i++)
            this.DS_points.push(this.mapcontainclass.History_points[i]);
        if (!this.runcarinterval) {
            this.InitDSCarInterval();
        }
    },
    DrawDSStep: function () {
        if (meiearthinstance.mapcontainclass.earthState == EnumEarthState.Unknown) {
            if (meiearthinstance.runcarinterval) {
                window.clearInterval(meiearthinstance.runcarinterval);
            }
            return;
        }
        if (meiearthinstance.DS_step <= meiearthinstance.DS_points.length - 1) {
            if (meiearthinstance.DS_step == 0) {
                var start = meiearthinstance.DS_points[0];
                meiearthinstance.DS_placemarks.push(meiearthinstance.DS_geHelpers.createPointPlacemark(start.loc,
                 { name: start.name, standardIcon: 'grn-diamond' }));
                meiearthinstance.FlyToLatLng(meiearthinstance.DS_points[meiearthinstance.DS_step].loc, meiearthinstance.DS_points[meiearthinstance.DS_step].distance);
                meiearthinstance.CreateDSCarInterval();
            }
            else if (meiearthinstance.DS_step == meiearthinstance.DS_points.length - 1) {
                var end = meiearthinstance.DS_points[meiearthinstance.DS_points.length - 1];
                var last = meiearthinstance.DS_placemarks[meiearthinstance.DS_placemarks.length - 1];
                meiearthinstance.DS_placemarks.push(meiearthinstance.DS_geHelpers.createPointPlacemark(end.loc,
                 { name: end.name, standardIcon: 'grn-diamond' }));
                if (meiearthinstance.runcarinterval) {
                    window.clearInterval(meiearthinstance.runcarinterval);
                }
                var routePlacemark = meiearthinstance.CreateHistoryLine([new google.maps.LatLng(last.getGeometry().getLatitude(), last.getGeometry().getLongitude()), end.loc], meiearthinstance);
                meiearthinstance.ge.getFeatures().appendChild(routePlacemark);
                meiearthinstance.DS_routes.push(routePlacemark);

                meiearthinstance.FlyToLatLng(meiearthinstance.DS_points[meiearthinstance.DS_step].loc, meiearthinstance.DS_points[meiearthinstance.DS_step].distance);
                meiearthinstance.DestoryDSCarInterval();
            }
            else {
                meiearthinstance.InitDSCar();
                var firstpoint = meiearthinstance.DS_points[meiearthinstance.DS_step - 1];
                var secondpoint = meiearthinstance.DS_points[meiearthinstance.DS_step];
                var step = meiearthinstance.DS_points[meiearthinstance.DS_step];
                var placemark = meiearthinstance.DS_geHelpers.createPointPlacemark(step.loc,
                 { name: step.name, standardIcon: 'red-circle' });
                meiearthinstance.DS_placemarks.push(placemark);
                var routePlacemark = meiearthinstance.CreateHistoryLine([firstpoint.loc, secondpoint.loc], meiearthinstance);
                meiearthinstance.ge.getFeatures().appendChild(routePlacemark);
                meiearthinstance.DS_routes.push(routePlacemark);


                firstpoint.duration = meiearthinstance.DS_speed * 7;
                secondpoint.duration = meiearthinstance.DS_speed * 7;
                meiearthinstance.RunDSCarInEarth([firstpoint, secondpoint]);
                meiearthinstance.FlyToLatLng(meiearthinstance.DS_points[meiearthinstance.DS_step].loc, meiearthinstance.DS_points[meiearthinstance.DS_step - 1].distance);
            }
            meiearthinstance.DS_step++;
        }
        else {
            if (meiearthinstance.runcarinterval) {
                window.clearInterval(meiearthinstance.runcarinterval);
            }
            return;
        }
    },
    InitDSCar: function () {
        if (this.DS_simulator) {
            this.DS_simulator.destroy();
            this.DS_simulator = null;
        }
    },

    InitDSCarInterval: function () {
        if (this.runcarinterval) {
            window.clearInterval(this.runcarinterval);

        }
        this.runcarinterval = window.setInterval(this.DrawDSStep, 500);
    },

    CreateDSCarInterval: function () {
        if (this.runcarinterval)
            window.clearInterval(this.runcarinterval);
        this.runcarinterval = window.setInterval(this.DrawDSStep, this.DS_speed * 2000);
    },

    DestoryDSCarInterval: function () {
        if (this.runcarinterval) {
            window.clearInterval(this.runcarinterval);
            this.runcarinterval = null;
        }
    },
    RunDSCarInEarth: function (pts) {
        this.DS_simulator = new DDSimulator(this.ge, pts);
        this.DS_simulator.initUI(null, true);
        this.DS_simulator.start(true);
    },

    FlyToLatLng: function (loc, distance) {
        var la = this.ge.createLookAt('');
        la.set(loc.lat(), loc.lng(), 10, this.ge.ALTITUDE_RELATIVE_TO_GROUND, 90, 0, distance * 2);
        this.ge.getView().setAbstractView(la);
    },

    DSCarSlower: function () {

        if (this.runcarinterval) {
            window.clearInterval(this.runcarinterval);
            this.runcarinterval = null;
        }
        this.DS_speed *= 2.0;
        this.CreateDSCarInterval();
    },

    DSCarFaster: function () {

        if (this.runcarinterval) {
            window.clearInterval(this.runcarinterval);
            this.runcarinterval = null;
        }
        this.DS_speed /= 2.0;
        this.CreateDSCarInterval();
    },

    DSCarPause: function () {
        if (this.DS_simulator) {
            this.DS_simulator.stop();
        }
        if (this.runcarinterval) {
            window.clearInterval(this.runcarinterval);
            this.runcarinterval = null;
        }
    },
    DSCarContinue: function () {
        if (this.DS_simulator) {
            this.DS_simulator.start();
        }
        this.CreateDSCarInterval();
    },
    DSCarGoFirstPoint: function () {
        if (this.ge) {
            this.DS_step = 0;
            var features = this.ge.getFeatures();
            for (var i = 0; i < this.DS_placemarks.length; i++) {
                features.removeChild(this.DS_placemarks[i]);
            }
            this.DS_placemarks.clear();
            for (var j = 0; j < this.DS_routes.length; j++) {
                features.removeChild(this.DS_routes[j]);
            }
            this.DS_routes.clear();
            this.InitDSCar();
            this.InitDSCarInterval();
        }

    },

    DSCarGoLastPoint: function () {
        this.DS_step = this.DS_points.length - 1;
    },

    ToInitDSCarForChangeMapType: function () {
        this.DSCarGoFirstPoint();
        this.DSCarPause();
        this.InitDSCar();
    },
    DrawHistoryPoint: function () {
        if (meiearthinstance.mapcontainclass.earthState == EnumEarthState.Done) {
            meiearthinstance.AddMarker(meiearthinstance.CreateEarthTrackerMarker(meiearthinstance.historypt.point, meiearthinstance.historypt.direction, meiearthinstance.historypt.name));
            if (meiearthinstance.interval) {
                window.clearInterval(meiearthinstance.interval);
                meiearthinstance.interval = null;
            }
        }
        else if (meiearthinstance.mapcontainclass.earthState == EnumEarthState.Unknown) return;
        else {
            if (!meiearthinstance.interval)
                meiearthinstance.interval = setInterval(meiearthinstance.DrawHistoryPoint, 2000);
        }
    },
    DrawAlarmPoint: function () {
        if (meiearthinstance.mapcontainclass.earthState == EnumEarthState.Done) {
            meiearthinstance.AddMarker(meiearthinstance.CreateEarthMarker(meiearthinstance.historypt.point, "images/caution.png", meiearthinstance.historypt.name));
            if (meiearthinstance.interval) {
                window.clearInterval(meiearthinstance.interval);
                meiearthinstance.interval = null;
            }
        }

        else if (meiearthinstance.mapcontainclass.earthState == EnumEarthState.Unknown) return;

        else {
            if (!meiearthinstance.interval)
                meiearthinstance.interval = setInterval(meiearthinstance.DrawAlarmPoint, 2000);
        }
    }
}
